var app = getApp();
const requireHttps = app.globalData.requireHttps //获取require域名
Page({

  data: {
    data_toggle: '',
    showView: true,
    carNumber: true,
    data_numCar: 0,
    parkingList: [],
    card: [],
    indeList: '',
    g_address: '',
    g_name: '',
    juli: '',
    g_usable: '',
    cardActive: '',
    gp_id: '',
    latitude: '',
    longitude: '',
    name: '',
    address: '',
    showParking: true,
    sname: '',
    price: '',
    itemsTime: []
  },

  onLoad: function(options) {
    this.timeList()
  },
  onShow: function() {
    // console.log(options)
    this.getParkingList()
    // this.addActive()
    this.getUserinfos()
    this.getGarageInfo()
  },
  reserve: function(e) {
    let _this = this
    _this.data.name = e.currentTarget.dataset.name
    _this.data.price = e.currentTarget.dataset.price
    _this.data.gp_id = e.currentTarget.dataset.gp_id

    // console.log(e)
    _this.setData({
      showView: (!_this.data.showView),
      name: e.currentTarget.dataset.name,
      price: e.currentTarget.dataset.price
    })
  },

  closeReserve: function(res) {
    let _this = this;
    _this.setData({
      showView: (!_this.data.showView)
    })
  },

  selectCarNum: function() {
    let _this = this;
    _this.setData({
      carNumber: (!this.data.carNumber)
    })
  },

  addActive: function(e) {
    let _this = this;

    _this.setData({
      data_numCar: e.currentTarget.dataset.numcar,
      cardActive: e.currentTarget.dataset.card
    })
  },
  //查看附近车库
  getParkingList: function() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let _this = this;
    //发起网络请求获取附近车库
    wx.request({
      url: '' + requireHttps + '/api/garage/getGarageParking',
      data: {
        g_id: app.globalData.gid,
        page: 1,
        token: app.globalData.token
      },
      method: 'post',
      success: res => { //请求成功
        // console.log(res)
        //存储车库id
        //存储附近车库数据
        // _this.data.gp_id = res.data.data.gp_id
        if (res.data.data == '') {
          _this.setData({
            noParking: '暂无车位',
            showParking: false
          })
        }
        _this.setData({
          parkingList: res.data.data,
          g_address: app.globalData.g_address,
          g_name: app.globalData.g_name,
          juli: app.globalData.juli,
          g_usable: app.globalData.g_usable
        })

        wx.request({
          url: '' + requireHttps + '/api/garage/getGarageInfo',
          data: {
            g_id: app.globalData.gid,
            lng: app.globalData.longitude,
            lat: app.globalData.latitude
          },
          method: 'post',
          success: res => {
            console.log(res)
            //存储当前车库经纬度
            _this.data.latitude = res.data.data.g_lat
            _this.data.longitude = res.data.data.g_lng
            _this.data.sname = res.data.data.gp_name
            _this.data.address = res.data.data.g_address
            _this.setData({
              min_lease_fee: res.data.data.min_lease_fee,
              overtime_fee: res.data.data.overtime_fee
            })
            wx.hideLoading()
          },
          fail: error => {
            console.log(error)
          }
        })
        
      },
      fail: error => { //请求失败
        console.log(error)
      }
    })
  },
  //创建预约订单
  createParkOrder: function () {
    // console.log(e.currentTarget.dataset.gp_id)
    let _this = this
    //发起网络请求创建预约订单
    wx.request({
      url: '' + requireHttps + '/api/pay/createParkingOrder',
      data: {
        token: app.globalData.token,
        gp_id: _this.data.gp_id,
        g_id: app.globalData.gid,
        license: _this.data.cardActive,
      },
      method: 'post',
      success: res => { //请求成功
        console.log(res)
        if (res.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            success: res => {
              if (res.confirm) {
                
              } else if (res.cancel) {

              }
            }
          })
          return false
        } else {

          wx.showModal({
            title: '提示',
            content: res.data.message,
            success: res => {
              if (res.confirm) {
                
              } else if (res.cancel) {

              }
            }
          })
          wx.navigateBack({
            delta: 2
          })
        }

        // _this.setData({
        //   name: res.data.data.g_name,
        //   address: res.data.data.g_address,
        //   latitude: res.data.data.latitude,
        //   longitude: res.data.data.longitude
        // })
      },
      fail: error => { //请求失败
        console.log(error)
        wx.showToast({
          title: '预约失败!',
        })
      }
    })
  },
  //获取预约车库信息
  getGarageInfo: function() {
    let _this = this

  },
  nowReserve: function(e) {
    let _this = this
    // console.log(e)
    let latitude = parseFloat(_this.data.latitude)
    let longitude = parseFloat(_this.data.longitude)
    // console.log(latitude)
    console.log(_this.data.sname)
    console.log(_this.data.address)
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18,
      name: e.currentTarget.dataset.name,
      address: e.currentTarget.dataset.address,
    })
  },
  goParking: function(e) {
    let _this = this
    console.log(e)
    let latitude = parseFloat(_this.data.latitude)
    let longitude = parseFloat(_this.data.longitude)
    // console.log(latitude)
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18,
      name: e.currentTarget.dataset.name,
      address: e.currentTarget.dataset.address
    })
  },
  //获取用户信息
  getUserinfos: function() {
    let _this = this
    //获取用户信息
    wx.request({
      url: '' + requireHttps + '/api/member/getMemberInfo',
      data: {
        token: app.globalData.token
      },
      method: 'post',
      success: res => {
        // console.log(res);
        //存储合作物业审核状态
        // _this.data.audit_status = res.data.data.audit_status;
        //存储我的车牌号
        // app.globalData.card = res.data.data.card;
        //存储客服电话
        // app.globalData.callServer = res.data.data.service_call;
        //存储用户余额
        app.globalData.balance = res.data.data.money;
        // _this.data.codeImgPath = res.data.data.code_img;
        //存储我的车位是否有数据
        _this.data.claim_num = res.data.data.claim_num
        // app.globalData.card = res.data.data.card
        _this.setData({
          card: res.data.data.card
        })
        console.log(res.data.data.card[0])
        _this.data.cardActive = res.data.data.card[0]
        console.log(_this.data.cardActive)
        // wx.hideLoading();
        //存储用户信息
        _this.setData({
          head: res.data.data.head,
          code_img: res.data.data.code_img,
          money: app.globalData.balance,
          phone: res.data.data.phone,
          service_call: res.data.data.service_call,
          sex: res.data.data.sex
        })
      }
    })
  },
  timeList: function() {
    let _this = this;
    //发起网络请求获取附近车库
    wx.request({
      url: '' + requireHttps + '/api/garage/getGarageParking',
      data: {
        g_id: app.globalData.gid,
        page: 1,
        token: app.globalData.token
      },
      method: 'post',
      success: res => { //请求成功
        // console.log(res)
        let data = res.data.data
        setInterval(() => {
          let itemsTime = []
          for (let i = 0; i < data.length; i++) {
            //获取当前时间
            let date = new Date()
            let startDate = new Date(data[i].o_start_time)
            let timeLag = (date / 1000 - startDate)
            let isTime = app.timeLag(timeLag)
            itemsTime.push({
              nowTime: isTime,
              status: data[i].gp_status
            })
            //判断是否push完成
            if (data.length == itemsTime.length) {
              _this.setData({
                itemsTime: itemsTime
              })
            }
          }
        }, 1000)
        setTimeout(() => {
          wx.hideLoading()
        }, 1000)
      }
    })
  }
})